from __future__ import annotations

import argparse
from gssgca.transmission_line.multi_weather_sag import sag_under_multiple_weathers


def main():
    parser = argparse.ArgumentParser(prog="gssgca", description="电力施工计算CLI")
    sub = parser.add_subparsers(dest="cmd")
    sag = sub.add_parser("sag")
    sag.add_argument("span", type=float)
    sag.add_argument("weight", type=float)
    cw = sub.add_parser("cableway")
    cw.add_argument("span", type=float)
    cw.add_argument("payload", type=float)
    win = sub.add_parser("winch")
    win.add_argument("max_tension", type=float)
    win.add_argument("--eta", type=float, default=0.9)
    win.add_argument("--sf", type=float, default=1.25)
    rv = sub.add_parser("reeving")
    rv.add_argument("hook", type=float)
    rv.add_argument("parts", type=int)
    rv.add_argument("--sheave_loss", type=float, default=0.02)
    rv.add_argument("--bearing_loss", type=float, default=0.01)
    rv.add_argument("--dead_end", type=float, default=0.98)
    cr = sub.add_parser("crossing")
    cr.add_argument("V", type=float, help="total vertical kN")
    cr.add_argument("H", type=float, help="total horizontal kN")
    cr.add_argument("span", type=float, help="beam span m")
    bs = sub.add_parser("bishop")
    bs.add_argument("json", type=str, help="path to slices json array")
    args = parser.parse_args()
    if args.cmd == "sag":
        pts = sag_under_multiple_weathers(
            span_m=args.span,
            unit_weight_kN_m=args.weight,
            elastic_modulus_MPa=73000,
            area_mm2=300,
            alpha_per_C=1.9e-5,
            temp_ref_C=15,
            initial_tension_kN=20,
            conductor_diameter_mm=21.6,
            weather_cases=[{"name": "ref"}],
        )
        for p in pts:
            print({"weather": p.weather, "tension": round(p.tension_kN,2), "sag": round(p.sag_m,2)})
    elif args.cmd == "cableway":
        from gssgca.transmission_line.cableway_selection import select_cableway
        s = select_cableway(args.span, args.payload)
        print({"diameter_mm": s.main_rope_diameter_mm, "breaking_kN": s.main_rope_breaking_kN, "sf": round(s.safety_factor,2)})
    elif args.cmd == "winch":
        from gssgca.lifting.winch_selection import select_winch
        w = select_winch(args.max_tension, args.eta, args.sf)
        print({"required_kN": round(w.required_line_pull_kN,2), "recommend_t": round(w.recommended_winch_t,2)})
    elif args.cmd == "reeving":
        from gssgca.lifting.block_detailed import reeving_analysis
        r = reeving_analysis(args.hook, args.parts, args.sheave_loss, args.bearing_loss, args.dead_end)
        print({"eta": round(r.efficiency,3), "running_kN": round(r.running_rope_tension_kN,2), "dead_end_kN": round(r.dead_end_tension_kN,2)})
    elif args.cmd == "crossing":
        from gssgca.crossing.system_analysis import analyze_crossing_system
        c = analyze_crossing_system(args.V, args.H, args.span)
        print({"column_kN": round(c.column_axial_kN,1), "beam_M_kNm": round(c.beam_bending_kNm,1), "guy_kN": round(c.guy_tension_kN,1)})
    elif args.cmd == "bishop":
        import json
        from gssgca.foundation.slope_bishop import bishop_simplified, BishopSlice
        with open(args.json, "r", encoding="utf-8") as f:
            data = json.load(f)
        res = bishop_simplified([BishopSlice(**d) for d in data])
        print({"Fs": round(res.factor_of_safety,3), "iters": res.iterations, "converged": res.converged})
    else:
        parser.print_help()


if __name__ == "__main__":
    main()


